﻿// <copyright file="UV2SourceFilesParser.cs" company="Horiba Medical">
//     Copyright (c) Horiba Medical. All rights reserved.
// </copyright>
// <summary>
//     Provides a source files parser for UV2 project files.
// </summary>

namespace DoxyOutParser.CodeCheck
{
	using System;
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.IO;

	/// <summary>
	/// Get sources from a UV2 project file.
	/// </summary>
	public class UV2SourceFilesParser : ISourceFilesParser
	{
		/// <summary>
		/// Path to the UV2 project file.
		/// </summary>
		private string uv2projectFile;

		/// <summary>
		/// Initializes a new instance of the UV2SourceFilesParser class.
		/// </summary>
		/// <param name="projectFile">The file to parse.</param>
		public UV2SourceFilesParser(string projectFile)
		{
			// TODO Check the value of string and eventually check if exist or not.
			this.uv2projectFile = projectFile;
		}

		#region ISourceFilesParser Members

		/// <summary>
		/// Gets the list of source files specified in the project file.
		/// </summary>
		/// <value>Not set available.</value>
		/// <returns>The generated list.</returns>
		public List<string> SourceFilesList
		{
			get
			{
				List<string> sourceFilesList = new List<string>();

				// Using of StreamReader to parse the file
				StreamReader uv2ProjectStreamReader = new StreamReader(this.uv2projectFile);
				string line = uv2ProjectStreamReader.ReadLine();

				while (line != null)
				{
					if (line.StartsWith("File", StringComparison.Ordinal))
					{
						int startIndex = line.IndexOf("<", StringComparison.Ordinal);
						int endIndex = line.IndexOf(">", StringComparison.Ordinal);
						if (startIndex != 0 && endIndex != 0 && (line.Contains(".s") == false))
						{
							sourceFilesList.Add(line.Substring(startIndex + 1, (endIndex - startIndex - 1)));
						}
					}

					line = uv2ProjectStreamReader.ReadLine();
				}

				return sourceFilesList;
			}
		}

		#endregion
	}
}
